import 'dart:io';

/// 目前先这样处理，
/// 第一次获取失败之后，通过ws发送重连
/// 不过理论上来说，客户端连接上ws之后，ip肯定已经有了
///

void main() async {
  // 创建 HTTP 服务器
  final server = await HttpServer.bind(InternetAddress.anyIPv4, 9999);
  print('Listening for connections on ${server.address.address}:${server.port}');

  await for (HttpRequest request in server) {
    // 获取客户端的 IP 地址
    String? clientIp = request.connectionInfo?.remoteAddress.address;
    clientIp ??= "获取失败";
    print("ip is $clientIp");

    // 构建响应
    request.response
      ..headers.contentType = ContentType.text
      ..write('Your public IP address is: $clientIp')
      ..close();
  }
}